<?php
/**
 * Created by PhpStorm.
 * User: ZT
 * Date: 2020/7/10 0010
 * Time: 11:59
 **/
namespace backend\controllers;

use backend\component\BackendBaseController;
use backend\component\ResultFormat;
use backend\service\AdminService;
use common\component\ClassFactory;
use common\models\base\Admin;

class UserController extends BackendBaseController
{
    public function actions()
    {
        return [
            "delete" =>[
                'class' => "backend\actions\DeleteAction",
                'modelName'=>Admin::className(),
                'is_physics' => true
            ],
            "swith" => [
                "class" => "backend\actions\SwithAction",
                "modelName" => Admin::className(),
                "field" => "status",
            ],
        ];
    }


    /**
     * 加载管理员视图
     */
    public function actionList()
    {
        $data =  AdminService::adminListData(\Yii::$app->request->post());
        return ResultFormat::success('SUCCESS',json_decode($data));
    }

    /**
     * 加载编辑视图
     */
    public function actionEdit() {
        $id = \Yii::$app->request->get("id",0);
        $model = Admin::modelById($id);
        return ResultFormat::success('SUCCESS',$model->attributes);
    }

    /**
     * 角色分配
     * @return string
     */
    public function actionRole() {
        $user_id = \Yii::$app->request->get("id",0);
        $roles = AdminService::roleByUid($user_id);
        $model = Admin::modelById($user_id);
        return $this->render("role",["roles"=>$roles,"model"=>$model]);
    }

    /**
     * 角色保存
     */
    public function actionTorole() {
        $user_id = \Yii::$app->request->get("id");
        $role_arr = \Yii::$app->request->post("role");
        $result = AdminService::modifyUserToRole($user_id,$role_arr);
        if ($result) {
            return ResultFormat::success("操作成功");
        }
        return ResultFormat::error("操作失败,请联系管理人");
    }

    /**
     * 修改密码
     */
    public function actionModify() {
            $params = \Yii::$app->request->post("form");
            $user_id = \Yii::$app->request->get("id");
            return AdminService::modifyUserPassword($user_id,$params);
    }
}


